package cn.topevery.garden.dal.dao.event;

import cn.topevery.garden.entity.po.event.EventInfoFile;
import cn.topevery.garden.entity.vo.event.EventInfoFileVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.entity.Value;
import hw.topevery.framework.enums.DbTypeEnum;
import hw.topevery.framework.util.DateUtil;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EventInfoFileDaoImpl extends BaseDaoImpl<EventInfoFile> implements EventInfoFileDao {
    @Override
    public <P extends DbCsp> List<EventInfoFile> getSearch(P para) {
        return null;
    }

    @Override
    public List<EventInfoFileVO> getEventInfoFilesByEvtId(String evtId) {
        Value<List<EventInfoFileVO>> res = new Value<>();
        String sql = "SELECT\n" +
                "\tef.*,\n" +
                "CASE\n" +
                "\t\tWHEN twai.c_activity_name IS NULL THEN\n" +
                "\t\ttwaib.c_activity_name ELSE twai.c_activity_name \n" +
                "\tEND AS c_act_inst_name \n" +
                "FROM\n" +
                "\tt_event_info_file ef\n" +
                "\tLEFT JOIN t_wf_activity_instance twai ON twai.c_activity_instance_id = ef.c_act_inst_id \n" +
                "\tAND twai.c_db_status = 0\n" +
                "\tLEFT JOIN t_wf_activity_instance_bak twaib ON twaib.c_activity_instance_id = ef.c_act_inst_id \n" +
                "\tAND twaib.c_db_status = 0 \n" +
                "WHERE\n" +
                "\tef.c_evt_id = :p_evtId \n" +
                "\tAND ef.c_db_status = 0";
        getDbExecute().run(sql, CommandType.Text, dbCmd -> {
            dbCmd.addInParameter("p_evtId", DbTypeEnum.VARCHAR, evtId);
            res.data = dbCmd.executeToList(EventInfoFileVO.class);
        });
        return res.data;
    }

    @Override
    public Boolean deleteByEvtId(String evtId) {
        Value<Boolean> res = new Value<>();
        String sql = "UPDATE t_event_info_file SET c_db_status = 1 WHERE c_evt_id = :p_evtId;";
        getDbExecute().run(sql, CommandType.Text, dbCmd -> {
            dbCmd.addInParameter("p_evtId", DbTypeEnum.VARCHAR, evtId);
            res.data = dbCmd.executeNonQuery();
        });
        return res.data;
    }

    /**
     * 获取今天的所有案件列表
     *
     * @return
     */
    @Override
    public List<EventInfoFile> getFilesByToday() {
        String table = "t_event_info_file teif";
        String columns = "teif.*";

        SqlQueryMate queryMate = new SqlQueryMate(table).setColumns(columns);
        queryMate
                .where(true, "teif.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "teif.c_create_time", ScriptConditionEnum.GreaterThanEq, String.format("%s 00:00:00", DateUtil.getToday()))
                .where(true, "teif.c_create_time", ScriptConditionEnum.LessThanEq, String.format("%s 23:59:59", DateUtil.getToday()))
        ;

        return super.selectList(queryMate, EventInfoFile.class);
    }

    @Override
    public List<EventInfoFile> getEntitiesByOneColumn(KeyValue<?> keyValue) {
        return super.getListEntityByOneColumn(keyValue);
    }

    @Override
    public Boolean deleteLastActFile(String evtId, String activityInstanceId) {
        Value<Boolean> res = new Value<>();
        String sql = "delete from t_event_info_file where c_evt_id=:p_evt_id and c_act_inst_id=:p_act_inst_id";
        run(sql, CommandType.Text, dbCommand -> {
            dbCommand.addInParameter("p_evt_id", DbTypeEnum.VARCHAR, evtId);
            dbCommand.addInParameter("p_act_inst_id", DbTypeEnum.VARCHAR, activityInstanceId);
            res.data = dbCommand.executeNonQuery();
        });
        return res.data;
    }
}
